add_executable(wclang wclang.cpp wclang_time.cpp)
install(TARGETS wclang DESTINATION bin)

option(SYMLINK_ALL_TRIPLETS "symlink all triplets" OFF)
set(SYMLINK_TRIPLETS ${VALID_TRIPLETS})
if(SYMLINK_ALL_TRIPLETS)
  set(SYMLINK_TRIPLETS ${TRIPLETS})
endif ()

list (INSERT SHORTCUTS 0 w32-clang w32-clang++ w64-clang w64-clang++)

foreach (SHORTCUT ${SHORTCUTS})
  install(CODE "set(FINAL_DIR ${CMAKE_INSTALL_PREFIX})
                message(STATUS \"Symlinking: \${FINAL_DIR}/bin/${SHORTCUT} -> wclang\")
                execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink wclang ${SHORTCUT} WORKING_DIRECTORY \${FINAL_DIR}/bin)")
endforeach ()

foreach (TRIPLET ${SYMLINK_TRIPLETS})
  install(CODE "set(FINAL_DIR ${CMAKE_INSTALL_PREFIX})
                if(NOT \"\$ENV{DESTDIR}\" STREQUAL \"\")
                  set(FINAL_DIR \$ENV{DESTDIR}${CMAKE_INSTALL_PREFIX})
                endif()
                message(STATUS \"Symlinking: \${FINAL_DIR}/bin/${TRIPLET}-clang -> wclang\")
                execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink wclang ${TRIPLET}-clang WORKING_DIRECTORY \${FINAL_DIR}/bin)
                message(STATUS \"Symlinking: \${FINAL_DIR}/bin/${TRIPLET}-clang++ -> wclang\")
                execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink wclang ${TRIPLET}-clang++ WORKING_DIRECTORY \${FINAL_DIR}/bin)")
endforeach ()

foreach (TRIPLET ${VALID_TRIPLETS})
  install(CODE "unset (MINGW_C_COMPILER CACHE)
                find_program (MINGW_C_COMPILER NAMES ${TRIPLET}-gcc)
                if (MINGW_C_COMPILER)
                  get_filename_component (MINGW_C_COMPILER_REALPATH ${MINGW_C_COMPILER} REALPATH)
                  get_filename_component (MINGW_PATH ${MINGW_C_COMPILER_REALPATH} PATH)
                  if (NOT EXISTS ${CLANG_PATH}/${TRIPLET}-gcc)
                    message(STATUS \"Symlinking: ${MINGW_PATH}/${TRIPLET}-* -> ${CLANG_PATH}\")
                    execute_process(COMMAND sh -c \"ln -sf ${MINGW_PATH}/${TRIPLET}-* ${CLANG_PATH}/\")
                  endif ()
                endif ()")
endforeach ()
